package com.jiangmiao.babaisyou.level;

import com.jiangmiao.babaisyou.constant.MapConstant;
import com.jiangmiao.babaisyou.constant.PeopleLocation;
import com.jiangmiao.babaisyou.domain.Block;
import com.jiangmiao.babaisyou.domain.People;
import com.jiangmiao.babaisyou.domain.Text;
import com.jiangmiao.babaisyou.util.GameUtil;
import com.jiangmiao.babaisyou.util.Location;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GamePeople {
    private List<People> peopleList;
    // 便于后续数据操作
    private Map<String,List<People>> peopleMap;

    public GamePeople() {
        this.peopleList = new ArrayList<>();
        this.peopleMap = new HashMap<>(); // 初始化

        /**
         * 4种可操作对象，之后可增加water等
         */
        this.peopleMap.put(PeopleLocation.BABA, new ArrayList<People>());
        this.peopleMap.put(PeopleLocation.ROCK, new ArrayList<People>());
        this.peopleMap.put(PeopleLocation.WALL, new ArrayList<People>());
        this.peopleMap.put(PeopleLocation.FLAG, new ArrayList<People>());

        Map<String, Object> stringObjectMap = null;
        try {
            stringObjectMap = GameUtil.loadMap("asset/map/level1");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, List<Location>> locationMap = (Map<String, List<Location>>)stringObjectMap.get(MapConstant.PEOPLE);

        for (String mapConstant : locationMap.keySet()) {
            List<Location> locations = locationMap.get(mapConstant);
            switch (mapConstant) {
                case MapConstant.BABA:
                    List<People> babaList = this.peopleMap.get(PeopleLocation.BABA);
                    for (int i = 0; i < locations.size(); i++) {
                        People baba = new People(locations.get(i), PeopleLocation.BABA);
                        babaList.add(baba);
                        this.peopleList.add(baba);
                    }
                    break;
                case MapConstant.FLAG:
                    List<People> flagList = this.peopleMap.get(PeopleLocation.FLAG);
                    for (int i = 0; i < locations.size(); i++) {
                        People flag = new People(locations.get(i), PeopleLocation.FLAG);
                        flagList.add(flag);
                        this.peopleList.add(flag);
                    }
                    break;
                case MapConstant.WALL:
                    List<People> wallList = this.peopleMap.get(PeopleLocation.WALL);
                    for (int i = 0; i < locations.size(); i++) {
                        People wall = new People(locations.get(i), PeopleLocation.WALL);
                        wallList.add(wall);
                        this.peopleList.add(wall);
                    }
                    break;
                case MapConstant.ROCK:
                    List<People> rockList = this.peopleMap.get(PeopleLocation.ROCK);
                    for (int i = 0; i < locations.size(); i++) {
                        People rock = new People(locations.get(i), PeopleLocation.ROCK);
                        rockList.add(rock);
                        this.peopleList.add(rock);
                    }
                    break;
            }
        }

//        People baba = new People(5 * 24, 9 * 24, PeopleLocation.BABA);
//        this.peopleList.add(baba);
//        this.peopleMap.get(PeopleLocation.BABA).add(baba);
//
//        People flag = new People(14 * 24, 9 * 24, PeopleLocation.FLAG);
//        this.peopleList.add(flag);
//        this.peopleMap.get(PeopleLocation.FLAG).add(flag);
//
//
//        People rock0 = new People(9 * 24, 8 * 24, PeopleLocation.ROCK);
//        People rock1 = new People(9 * 24, 9 * 24, PeopleLocation.ROCK);
//        People rock2 = new People(9 * 24, 10 * 24, PeopleLocation.ROCK);
//        this.peopleList.add(rock0);this.peopleMap.get(PeopleLocation.ROCK).add(rock0);
//        this.peopleList.add(rock1);this.peopleMap.get(PeopleLocation.ROCK).add(rock1);
//        this.peopleList.add(rock2);this.peopleMap.get(PeopleLocation.ROCK).add(rock2);
//
//
//        for (int i = 4; i < 15; i++) {
//            People wall0 = new People(i * 24, 7 * 24, PeopleLocation.WALL);
//            People wall1 = new People(i * 24, 11 * 24, PeopleLocation.WALL);
//            this.peopleList.add(wall0);this.peopleMap.get(PeopleLocation.WALL).add(wall0);
//            this.peopleList.add(wall1);;this.peopleMap.get(PeopleLocation.WALL).add(wall1);
//        }
    }

    public void draw(Graphics graphics) {
        for (int i = 0; i < this.peopleList.size(); i++) {
            People people = peopleList.get(i);
            graphics.drawImage(people.getImage(),people.getX(),people.getY(),null);
        }
    }

    public void move(int direction, Block you, List<String> stop, List<String> push, List<String> win , GameText gameText) {
        /**
         * 先移动
         */
        switch (direction) {
            case 1:
                you.up = true;
                you.down = you.right = you.left = false;
                you.setY(you.getY() - 24);//y -= 24;
                break;
            case -1:
                you.down = true;
                you.up = you.right = you.left = false;
                you.setY(you.getY() + 24);//y += 24;
                break;
            case 2:
                you.right = true;
                you.up = you.down = you.left = false;
                you.setX(you.getX() + 24);//x += 24;
                break;
            case -2:
                you.left = true;
                you.up = you.right = you.down = false;
                you.setX(you.getX() - 24);//x -= 24;
                break;
        }

        /**
         * stop逻辑
         */
        if (stop != null && stop.size() > 0) {
            for (int i = 0; i < stop.size(); i++) {
                String s = stop.get(i);
                List<People> stopList = this.peopleMap.get(s);

                for (int j = 0; j < stopList.size(); j++) { // 遍历所有不可动的对象判断
                    People stopPeople = stopList.get(j);
                    if(stopPeople.getX() == you.getX() && stopPeople.getY() == you.getY()) { // 发生碰撞
                        // 反弹
                        if (you.up) {
                            you.setY(you.getY() + 24);//y += 24;
                        } else if (you.down) {
                            you.setY(you.getY() - 24);//y -= 24;
                        } else if (you.right) {
                            you.setX(you.getX() - 24);//x -= 24;
                        } else if (you.left) {
                            you.setX(you.getX() + 24);//x += 24;
                        }
                        return;// 优化
                    }
                }

            }
        }


        /**
         * push逻辑
         */
        if (push != null && push.size() > 0) {
            for (int i = 0; i < push.size(); i++) {
                String s = push.get(i);

                List<People> pushList = this.peopleMap.get(s);
                for (int j = 0; j < pushList.size(); j++) { // 遍历所有Push的对象判断
                    People pushPeople = pushList.get(j);
                    if (pushPeople == you) continue;
                    if (pushPeople.getX() == you.getX() && pushPeople.getY() == you.getY()) { // 发生碰撞
                        move(direction, pushPeople, stop, push,win, gameText);
                    }
                }
            }
        }


        /**
         * text推动逻辑
         */
        List<Text> textList = gameText.getTextList();
        for (int i = 0; i < textList.size(); i++) {
            Text text = textList.get(i);
            if(text == you) continue;
            if(text.getX() == you.getX() && text.getY() == you.getY()) {
                move(direction, text, stop, push,win, gameText);
                return;
            }
        }


        /**
         * win逻辑
         */
        if(win != null && win.size() > 0) {
            for (int i = 0; i < win.size(); i++) {
                String s = win.get(i);

                List<People> winList = this.peopleMap.get(s);
                for (int j = 0; j < winList.size(); j++) {
                    People winPeople = winList.get(j);
                    if(winPeople.getX() == you.getX() && winPeople.getY() == you.getY()) {
                        System.out.println("胜利了");
                        return;
                    }
                }
            }
        }


    }

    public List<People> getPeopleList() {
        return peopleList;
    }


}
